from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from django.urls import reverse
from .models import *
# Create your views here.

def login(request):

    context = {}
    return render(request,'login/login.html',context)

def login_yunsuan(request):
    info = request.POST
    username = info['username']
    pswd = info['password']

    try:
        info_obj = PersonID.objects.get(person_name = username)
    except PersonID.DoesNotExist:
        return HttpResponse('用户名不存在')
    else:
        if info_obj.person_pswd == None:
            if pswd == info_obj.person_id[-6:]:
                if info_obj.person_sf.person_sf == '学生':
                    request.session['username'] = username
                    request.session['权限'] = '学生'
                    return HttpResponseRedirect(reverse('paper:dati'))
                
                elif info_obj.person_sf.person_sf == '老师':
                    request.session['username'] = username
                    request.session['权限'] = '老师'
                    return HttpResponseRedirect(reverse('paper:index'))
                #教务登录后可注册管理账号
                elif info_obj.person_sf.person_sf == '教务':
                    request.session['username'] = username
                    request.session['权限'] = '教务'
                    return HttpResponseRedirect(reverse('paper:index'))
            else:
                context = {'message':'您输入的账号或密码不正确!'}
                return render(request,'login/login.html',context)
        elif pswd == info_obj.person_pswd:
            if info_obj.person_sf.person_sf == '学生':
                request.session['username'] = username
                request.session['权限'] = '同学'
                return HttpResponseRedirect(reverse('paper:dati'))
            elif info_obj.person_sf.person_sf == '老师':
                request.session['username'] = username
                request.session['权限'] = '老师'
                return HttpResponseRedirect(reverse('paper:index'))
                #教务登录后可注册管理账号
            elif info_obj.person_sf.person_sf == '教务':
                request.session['username'] = username
                request.session['权限'] = '教务'
                return HttpResponseRedirect(reverse('paper:index'))
        else:
            context = {'message':'您输入的账号或密码不正确！'}
            return render(request,'login/login.html',context)
            
def change_pswd(request):

    context = {}
    return render(request,'login/change.html',context)

def change_pswd_yunsuan(request):
    info = dict(request.POST)
    print(info)
    pswd_list = info['password']
    print(pswd_list[0])
    try:
        person_obj = PersonID.objects.get(person_name = info['username'][0])
    except PersonID.DoesNotExist:
        context = {'message':'账号不存在'}
        return render(request,'login/change.html',context)
    else:
        if person_obj.person_pswd == pswd_list[0]:
            if pswd_list[1] == pswd_list[2] and 6 <= len(pswd_list[1]) <= 16:
                person_obj.person_pswd = pswd_list[1]
                person_obj.save()
                context = {'message':'密码修改成功'}
                return render(request,'login/change.html',context)
            else:
                context = {'message':'请检查你的密码，是否相同，或者符合长度规定'}
                return render(request,'login/change.html',context)
        elif person_obj.person_pswd == None:
            if pswd_list[1] == pswd_list[2] and person_obj.person_id[-6:] == pswd_list[0]\
               and 6 <= len(pswd_list[1]) <= 16:
                person_obj.person_pswd = pswd_list[1]
                person_obj.save()
                context = {'message':'密码修改成功'}
                return render(request,'login/change.html',context)
            else:
                context = {'message':'请检查你的密码，是否相同，或者符合长度规定'}
                return render(request,'login/change.html',context)
        else:
            context = {'message':'账号或密码输入有误！'}
            return render(request,'login/change.html',context)
    
                    
























